﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using 聚欣订单分析.PublicClass;
using System.Collections.Generic;

namespace 聚欣订单分析
{
    public partial class epOffer : System.Web.UI.Page
    {
        PLSQLClass plsql = new PLSQLClass();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["qtqx"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else if (Session["qtqx"].ToString().IndexOf("") < 0)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    SqlDataSourceSQL();   
                }
                //AspxGridView回调加载数据
                if (ASPxGridView1.IsCallback)
                {
                    SqlDataSourceSQL();   
                }
            }
        }

        private void SqlDataSourceSQL()
        {
            string strWb = plsql.SqlReaderData("select top 1 pwbn from tbl_tempOffer where userID=" + Session["UserID"].ToString() + "and pname = ''");
            if (strWb != "")
            {
                
                ASPxSpinEdit1.Text = strWb;

                SqlDataSource3.SelectCommand = "SELECT *,ProMoney*pnum as muProMoney,pnum*(ProMoney*zk*" + strWb + ") as zkzj FROM [v_tempOffer] where userID=" + Session["UserID"].ToString() + "and pname = ''";
                lblInfo.Text = "当前维保比例" + strWb;
            }
            else
            {
                lblInfo.Text = "";
            }
        }

        //添加
        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            List<object> keyValues = this.grid.GetSelectedFieldValues("ID");
            if (keyValues.Count > 0)
            {
                for (int i = 0; i < keyValues.Count; i++)
                {
                    if (plsql.SqlBind("select * from tbl_tempOffer where pname='' and pid=" + keyValues[i].ToString() + " and userID=" + Session["UserID"].ToString()).Rows.Count == 0)
                    {
                        string strWb = plsql.SqlReaderData("select top 1 pwbn from tbl_tempOffer where userID=" + Session["UserID"].ToString() + "and pname = ''");
                        if (strWb == "")
                        {
                            strWb = ASPxSpinEdit1.Text;
                        }
                        plsql.SqlGetData("insert into tbl_tempOffer values(" + keyValues[i].ToString() + "," + Session["UserID"].ToString() + "," + seNum.Text + ",''," + strWb + ")");
                    }
                    else
                    {
                        plsql.SqlGetData("update tbl_tempOffer set pnum+=" + seNum.Text + " where  pname='' and pid=" + keyValues[i].ToString() + " and userID=" + Session["UserID"].ToString());
                    }
                }
                grid.Selection.UnselectAll();
                SqlDataSourceSQL();
                ASPxGridView1.DataBind();
            }
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ASPxDelete_Click(object sender, EventArgs e)
        {
            ASPxSpinEdit1.Text = "1.17";
            lblInfo.Text = "当前维保比例1.17";
            plsql.SqlBind("Delete tbl_tempOffer where userID=" + Session["UserID"].ToString() + " and pname=''");
            ASPxGridView1.DataBind();
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnXls_Click(object sender, EventArgs e)
        {
            SqlDataSourceSQL();
            ASPxGridViewExporter1.WriteXlsxToResponse("报价模板");
        }

        /// <summary>
        /// 保存模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddp_Click(object sender, EventArgs e)
        {
            if (txtPname.Text.Trim() != "")
            {
                if (plsql.SqlReaderData("select count(*) from tbl_tempOffer  where pname='" + txtPname.Text.Trim() + "' and userID=" + Session["UserID"].ToString()) == "0")
                {
                    plsql.SqlGetData("update tbl_tempOffer set pname='" + txtPname.Text.Trim() + "' where pname='' and userID=" + Session["UserID"].ToString());
                    cbPname.DataBind();
                    ASPxGridView1.DataBind();
                    lblInfo.Text = "";
                    ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('保存成功!')</script>");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('模板名称已经存在!')</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('模板名称不能为空!')</script>");
            }
        }

        /// <summary>
        /// 读取模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bthReard_Click(object sender, EventArgs e)
        {
            string strPname = cbPname.Text.Trim();
            if (strPname != "")
            {
                plsql.SqlBind("Delete tbl_tempOffer where userID=" + Session["UserID"].ToString() + " and pname=''");
                DataTable dt = plsql.SqlBind("select * from tbl_tempOffer where userID=" + Session["UserID"].ToString() + " and pname='" + strPname + "'");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    plsql.SqlGetData("insert into tbl_tempOffer values(" + dt.Rows[i]["pid"].ToString() + "," + Session["UserID"].ToString() + "," + dt.Rows[i]["pnum"].ToString() + ",''," + dt.Rows[i]["pwbn"].ToString() + ")");
                }
                SqlDataSourceSQL();
                ASPxGridView1.DataBind();
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('请选择要读取的模板!')</script>");
            }
        }

        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeletP_Click(object sender, EventArgs e)
        {
            string strPname = cbPname.Text.Trim();
            if (strPname != "")
            {
                plsql.SqlBind("Delete tbl_tempOffer where userID=" + Session["UserID"].ToString() + " and pname='" + strPname + "'");
                cbPname.Text = "";
                cbPname.DataBind();
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script>alert('请选择要删除的模板!')</script>");
            }
        }

        /// <summary>
        /// 修改维保
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ASPxButton2_Click(object sender, EventArgs e)
        {
            string strWb = plsql.SqlReaderData("select top 1 pwbn from tbl_tempOffer where userID=" + Session["UserID"].ToString() + "and pname = ''");
            if (strWb != "")
            {
                plsql.SqlGetData("update tbl_tempOffer set pwbn=" + ASPxSpinEdit1.Text + " where pname='' and userID=" + Session["UserID"].ToString());
                SqlDataSourceSQL();
                ASPxGridView1.DataBind();
            }
        }
    }
}
